import pandas as pd
from keras.saving.save import load_model
from sklearn.preprocessing import LabelEncoder, StandardScaler

titanic_survival_model_filepath = 'models/titanic_survival_model.h5'
titanic_survival_model = load_model(titanic_survival_model_filepath)

test_file_path = 'data/test.csv'
data_test = pd.read_csv(test_file_path)
test_result_file_path = 'data/test_result.csv'
data_test_result = pd.read_csv(test_result_file_path)

# 重新排列列的顺序
data_test = data_test[['PassengerId', 'Sex', 'SibSp', 'Parch', 'Pclass']]
label_encoder_sex = LabelEncoder()
data_test.iloc[:, 1] = label_encoder_sex.fit_transform(data_test.iloc[:, 1])

X_test = data_test.iloc[:, 0:5]

sc = StandardScaler()
X_test = sc.fit_transform(X_test)

prediction = titanic_survival_model.predict(X_test).tolist()
prediction_se = pd.Series(prediction)

data_test_result['prediction'] = prediction_se
data_test_result['prediction'] = data_test_result['prediction'].str.get(0)

series = []
for val in data_test_result.prediction:
    if val >= 0.5:
        series.append(1)
    else:
        series.append(0)

data_test_result['result'] = series

match_count = 0
no_match_count = 0
for val in data_test_result.values:
    if val[1] == val[3]:
        match_count += 1
    else:
        no_match_count += 1

print("Test result: ")
print(f"Match count: {match_count}, \r\nNo match count: {no_match_count}")